package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.KeyTrainerStatistics;
import com.beerandjesus.sportofkings.handicapper.model.MaidenHorseAnalysisResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.RaceType;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.KeyTrainerStatisticsCategory;
import com.beerandjesus.sportofkings.handicapper.strategy.RacingCalculator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TrainerFirstOrSecondStarterResolver {
    public static final Float ELEVEN = new Float(11.0);
    private static final Map<String, KeyTrainerStatisticsCategory> STAT_MAP = new HashMap<String, KeyTrainerStatisticsCategory>();
    private RacingCalculator calc = new RacingCalculator();

    static {
        STAT_MAP.put(RaceType.S.toString() + "0", KeyTrainerStatisticsCategory.STAT_1ST_TIME_STR);
        STAT_MAP.put(RaceType.S.toString() + "1", KeyTrainerStatisticsCategory.STAT_2ND_CAREER_RACE);
    }

    public KeyTrainerStatistics getKeyTrainerStatistics(
            List<KeyTrainerStatistics> statList, int numStarts) {
        KeyTrainerStatisticsCategory category = STAT_MAP.get(RaceType.S.toString() + numStarts);
        for (KeyTrainerStatistics stats : statList) {
            if (stats.getCategory() != null && stats.getCategory().equals(category)) {
                return stats;
            }
        }
        return null;
    }

    public void resolveTrainerWinPercentage(MaidenHorseAnalysisResultImpl result, Horse h) {
        String trainer = "---";
        List<KeyTrainerStatistics> statList = h.getTrainerJockeyOwner().getKeyTrainerStatistics();
        KeyTrainerStatistics stats = getKeyTrainerStatistics(statList,
                h.getNumberOfStarts());
        if (stats != null) {
            Float f = calc.getFloatOrZero(stats.getWinPercentage());
            if (f.compareTo(ELEVEN) >= 0) {
                trainer = f.intValue() + "%";
            }
        }
        result.setTrainerWinPercentage(trainer);
    }


}
